import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Scanner;


public class Invitacion {

	private File entrada;
	private File salida;
	private String front;
	private String body;
	private String back;
	private BufferedWriter out;
	
	public Invitacion () throws IOException{
		entrada = new File("invitation.xml");
		salida = new File("invcss.html");
		if (salida.exists()) {
			salida.delete();
			salida.createNewFile();
		}
		out = new BufferedWriter(new FileWriter(salida));
	}
	
	public Invitacion (String fichin, String fichout) throws IOException{
		entrada = new File(fichin);
		salida = new File(fichout);
		if (salida.exists()) {
			salida.delete();
			salida.createNewFile();
		}
		out = new BufferedWriter(new FileWriter(salida));
	}
	
	private void WriteFront () throws IOException {
		String to = null;
		String date = null;
		String where = null;
		String why = null;
	    out.write("<TABLE>\n<TBODY>\n");
		Scanner reader = new Scanner (front);
		String aux;

		while (true) {
			reader.useDelimiter("<to>|<date>|<where>|<why>");
			reader.next();
			if (!reader.hasNext())
				break;
			reader.useDelimiter("</to>|</date>|</where>|</why>");
			aux = reader.next();
			if (aux.regionMatches(0,"<to>",0,4))
				out.write("<TR><TD class=\front\">To: </TD>\n<TD>" + to + "</TD></TR>\n");
			else
				if (aux.regionMatches(0,"<date>",0,6))
					out.write("<TR><TD class=\front\">When: </TD>\n<TD>" + date + "</TD></TR>\n");
				else
					if (aux.regionMatches(0,"<where>",0,7))
						out.write("<TR><TD class=\front\">Venue: </TD>\n<TD>" + where + "</TD></TR>\n");
					else
						if (aux.regionMatches(0,"<why>",0,5))
							out.write("<TR><TD class=\front\">Occasion: </TD>\n<TD>" + why + "</TD></TR>\n");
		}
		out.write("</TBODY>\n</TABLE>\n");
		reader.close();
	}
	
	private void WriteBody () throws IOException {
		String aux;
		aux = body.replaceAll("<par>", "<P>");
		aux = aux.replaceAll("</par>", "</P>");
		aux = aux.replaceAll("<emph>", "<EM>");
		aux = aux.replaceAll("</emph>", "</EM>");
		out.write(aux);
	}
	
	private void WriteBack () throws IOException  {
		String signature;
		Scanner scanner = new Scanner(back);
		scanner.useDelimiter("<signature>|</signature>");
		scanner.next();
		signature = scanner.next();
		scanner.close();
		
		out.write("<P CLASS=\"signature>\">");
		out.write(signature);
		out.write("</P>\n");
	}
	
	private void Read () throws IOException {
		Scanner reader = new Scanner (entrada);
		String aux;
		reader.useDelimiter("<front>|<body>|<back>");
		while (true) {
			reader.next();
			if (!reader.hasNext())
				break;
			reader.useDelimiter("</front>|</body>|</back>");
			aux = reader.next();
			if (aux.regionMatches(0,"<front>",0,6)) {
				front = aux;
				WriteFront();
			}
			else {
				if (aux.regionMatches(0,"<body>",0,6)) {
					body = aux;
					WriteBody();
				}
				else {
					if (aux.regionMatches(0,"<back>",0,6)) {
						back = aux;
						WriteBack();
					}
				}
			}
			reader.useDelimiter("<front>|<body>|<back>");
		}
		reader.close();
	}
	
	public static void main(String[] args) throws IOException {
		Invitacion i = new Invitacion ();
		//Invitacion i = new Invitacion (args[0], args[1]);
		out.write("<HTML>\n<HEAD>\n");
		out.write("<TITLE> Invitation (sgmlpl/CSS formatting) </TITLE>\n");
		out.write("<LINK href=\"invit.css\" rel=\"style-sheet\" type=\"text/css\">\n");
		out.write("</HEAD>\n<BODY>\n");
		out.write("<H1>INVITATION</H1>\n");
		i.Read();
		out.write("</BODY>\n</HTML>\n");
		out.close();
	}

}
